<?php

namespace fast;


use Exception;


class Office2Pdf
{

    /**
     * word2pdf() 执行odconverter用openOffice转pdf代码 linux系统下
     * @param $infile_path
     * @param $outfile_path
     * @param $jodconverter_path
     * @return bool
     */
    public function word2pdf($infile_path, $outfile_path, $jodconverter_path)
    {
        if (empty($infile_path)) return false;
        try {
            $set_charset = 'export LANG=en_US.UTF-8;'; //为防止中文文件转pdf时exec()函数执行出错 设置字符集为utf-8
            $p = "java -jar " . $jodconverter_path . ' ' . $infile_path . ' ' . $outfile_path;
			echo $p;die;
            exec($set_charset . $p);
        } catch (Exception $e) {
            return false;
        }
        return true;
    }

    /**
     * 获取PDF文件页数的函数获取  两系统通用
     * 文件应当对当前用户可读（linux下）
     * @param  [string] $path [文件路径]
     * @return int
     */
    public function getPdfPages($outfile_path)
    {
        if (!file_exists($outfile_path)) return 0;
        if (!is_readable($outfile_path)) return 0;
        // 打开文件
        $fp = @fopen($outfile_path, "r");
        if (!$fp) {
            return 0;
        } else {
            $max = 0;
            while (!feof($fp)) {
                $line = fgets($fp, 255);
                if (preg_match('/\/Count [0-9]+/', $line, $matches)) {
                    preg_match('/[0-9]+/', $matches[0], $matches2);
                    if ($max < $matches2[0]) $max = $matches2[0];
                }
            }
            fclose($fp);
            // 返回页数
            return $max;
        }
    }


    /**
     * office文件转pdf输出方法
     * @param $file_path
     * @return string
     */
    public function office2pdf($file_path, $filename)
    {
        $rand_path = $filename . '.pdf';
        //判断服务器操作系统 暂定为win和linux两种
        if (PHP_OS == 'Linux') {
            $jodconverter_path = "/opt/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar";
            $infile_path = "/www/wwwroot/project/lcj/wenzhang/public/" . $file_path;
            $outfile_path = "/www/wwwroot/project/lcj/wenzhang/public/office2pdf/" . $rand_path;
            $this->word2pdf($infile_path, $outfile_path, $jodconverter_path);
            return "/office2pdf/" . $rand_path;
        } elseif (PHP_OS == 'WINNT') {
            $con = new Office2Pdf();
            $infile_path = APP_PATH . "../public/" . $file_path;
            $outfile_path = APP_PATH . "../public/office2pdf/" . $rand_path;
            $con->run($infile_path, $outfile_path);
            return "/office2pdf/" . $rand_path;
        }
    }

    /**
     * win系统下操作openoffice方法
     * @var COM
     */
    private $osm;

    public function __construct()
    {
        if (PHP_OS == 'WINNT') {
            $this->osm = new \COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.n");
        }
    }

    public function MakePropertyValue($name, $value)
    {
        $oStruct = $this->osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
        $oStruct->Name = $name;
        $oStruct->Value = $value;
        return $oStruct;
    }

    public function transform($input_url, $output_url)
    {
        $args = array($this->MakePropertyValue("Hidden", true));
        $oDesktop = $this->osm->createInstance("com.sun.star.frame.Desktop");
        $oWriterDoc = $oDesktop->loadComponentFromURL($input_url, "_blank", 0, $args);
        $export_args = array($this->MakePropertyValue("FilterName", "writer_pdf_Export"));
        $oWriterDoc->storeToURL($output_url, $export_args);
        $oWriterDoc->close(true);
        return $this->getPdfPages($output_url);
    }

    public function run($input, $output)
    {
        $input = "file:///" . str_replace("\\", "/", $input);
        $output = "file:///" . str_replace("\\", "/", $output);
        return $this->transform($input, $output);
    }


}

